from app.routes import StudentRoute
from app.views.ClasView import ClasView
class StudentView:
    def __init__(self):
        self.function_list = ["查看学生信息", "添加学生信息", "修改学生信息", "删除学生信息"]
        self.clas_view = ClasView()
    def display_menu(self):
        print("\n============== 学生成绩管理系统-学生管理 ==============")
        for idx, func in enumerate(self.function_list):
            print(f"  {idx + 1}. {func}")
        print("  5. 返回")
        print("请根据上面的选项输入数字并按回车继续...")

    def get_student(self):
        """
        获取班级信息
        :return:
        """
        keyword = input("请输入学生姓名关键字，回车搜索全部：")
        # 查询所有的班级信息
        resp = StudentRoute.get_by_keyword(keyword)
        # 打印查询到的信息
        print(resp["message"])
        for data in resp["data"]:
            print(data)

    def add_student(self):
        self.clas_view.get_clases()
        clas_id = input("请输入班级id：")
        name = input("请输入学生姓名：")
        gender = input("请输入学生性别：")
        resp = StudentRoute.add_student(clas_id, name, gender)
        print(resp["message"])

    def update_student(self):
        self.get_student()
        stu_id = input("请输入要修改的学生id:")
        new_name = input("请输入新的学生姓名，回车不修改:")
        new_gender = input("请输入新的学生性别，回车不修改:")
        # 打印班级列表
        self.clas_view.get_clases()
        new_clas_id = input("请输入新的班级id，回车不修改:")
        resp = StudentRoute.update_student(stu_id, new_name, new_gender, new_clas_id)
        print(resp["message"])

    def delete_student(self):
        self.get_student()
        id = input("请输入要删除的学生id:")
        resp = StudentRoute.delete_student(id)
        print(resp["message"])
    def run(self):
        self.display_menu()
        choice = input("请输入操作符：")
        if choice == "1":
            self.get_student()
            self.run()
        elif choice == "2":
            self.add_student()
            self.run()
        elif choice == "3":
            self.update_student()
            self.run()
        elif choice == "4":
            self.delete_student()
            self.run()
        elif choice == "5":
            pass
        else:
            print("请输入正确的操作符")